package com.alison;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setAppName("wc").setMaster("local");
        JavaSparkContext javaSparkContext = new JavaSparkContext(conf);

        String filePath = "D:\\workspace\\lab\\learnbigdata\\learnspark\\sparkcore\\src\\main\\resources\\word.txt";
        JavaRDD<String> lines = javaSparkContext.textFile(filePath);
        JavaRDD<String> words = lines.flatMap(s -> Arrays.asList(s.split(" ")).iterator());
        JavaPairRDD<String, Integer> map = words.mapToPair(s -> new Tuple2<>(s, 1));
        JavaPairRDD<String, Integer> wordToCount = map.reduceByKey((x, y) -> x + y);
        wordToCount.collect().forEach(System.out::println);
        javaSparkContext.close();
    }
}
